# Copyright (c) 2008, RPI-ACM, Jesse Kempf <jessekempf@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# - Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
# - Neither the names of RPI-ACM nor the names of its contributors
#     may be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
#  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
#  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Simple extended assertions for use in component testing."""
class AssertionFailure(Exception):
	"""The asserted expression is false."""
	pass

def assert_(predicate, msg="", call=True):
	"""Assert predicate. If call is true and predicate is callable,
	predicate(). Display the optional message if the assert fails."""
	if call and callable(predicate):
		predicate = predicate()
	if not predicate:
		raise AssertionFailure(msg)

	return predicate

def assert_not(predicate, msg="", call=True):
	"""Assert !predicate. If call is true and predicate is callable, test
	!predicate(). Display the optional message if the assert fails."""
	if call and callable(predicate):
		predicate = predicate()
	if predicate:
		raise AssertionFailure(msg)
	return predicate

def assert_raises(predicate, exc, msg=""):
	"""Assert predicate() raises the provided exception. The given function
	must take no arguments. Lambdas should be used if arguments are needed.
	If predicate() fails to raise the given exception, display the optional
	message."""

	try:
		predicate()
	except exc:
		return
	raise AssertionFailure(msg)

def assert_does_not_raise(predicate, exc, msg="", expect=None):
	"""Assert predicate() does not raise the provided exception. The given
	function must take no arguments. Lambdas should be used if arguments
	are needed. If predicate() fails to raise the given exception, display
	the optional message. A list of exceptions to ignore may be provided."""

	expect = expect if expect is not None else []

	try:
		return predicate()
	except exc: 
		raise AssertionFailure(msg)
	except expect:
		pass

def run_tests(*args):
	"Run the provided tests."""
	for test in args:
		try:
			test()
		except AssertionFailure:
			import inspect, traceback
			traceback.print_exc(limit=(len(inspect.stack())))
